1 package activity;
2
3 import java.lang.*;
4 import javax.swing.*;
5 import java.awt.*;
6 import javax.swing.border.*;
7 import java.awt.event.*;
8 import attr.*;
9
10 public class AddProductActivity extends JFrame implements ActionListener {
11 private JPanel panel;
12 private ViewProductActivity activity;
13 private Employee employee;
14 private JButton buttonLogout, buttonBack, buttonAdd;
15 private JLabel title, header, productNameLabel, productQtLabel, productPriceLabel;
16 private JTextField productNameTF, productQtTF, productPriceTF;
17
18 public AddProductActivity(ViewProductActivity prev, Employee employee) {
19 super("Add Product");
20
21 this.setSize(Theme.GUI_WIDTH, Theme.GUI_HEIGHT);
22 this.setResizable(false);
23 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
24 this.setLocationRelativeTo(null);
25 this.activity = prev;
26 this.employee = employee;
27
28 panel = new JPanel();
29 panel.setLayout(null);
30 panel.setBackground(Theme.BACKGROUND_PANEL);
31
32 title = new JLabel("Add Product");
33 title.setBounds(30, 40, 280,75);
34 title.setOpaque(true);
35 title.setBorder(new EmptyBorder(0,20,0,0));
36 title.setFont(Theme.FONT_TITLE);
37 title.setForeground(Theme.COLOR_TITLE);
38 panel.add(title);
39
40 buttonLogout = new JButton("Logout");
41 buttonLogout.setBounds(Theme.GUI_WIDTH-140, 40, Theme.BUTTON_PRIMARY_WIDTH,30);
42 buttonLogout.setFont(Theme.FONT_BUTTON);
43 buttonLogout.setBackground(Color.WHITE);
44 buttonLogout.setForeground(Theme.COLOR_TITLE);
45 buttonLogout.addActionListener(this);
46 panel.add(buttonLogout);
47
48 buttonBack = new JButton("Back");
49 buttonBack.setBounds(Theme.GUI_WIDTH-140, 80, Theme.BUTTON_PRIMARY_WIDTH,30);
50 buttonBack.setFont(Theme.FONT_BUTTON);
51 buttonBack.setBackground(Theme.BACKGROUND_BUTTON_PRIMARY);
52 buttonBack.setForeground(Theme.COLOR_BUTTON_PRIMARY);
53 buttonBack.addActionListener(this);
54 panel.add(buttonBack);
55
56 productNameLabel = new JLabel("Name: ");
57 productNameLabel.setBounds(60, 190, 140, 30);
58 productNameLabel.setFont(Theme.FONT_REGULAR);
59 panel.add(productNameLabel);
60
61 productPriceLabel = new JLabel("Price: ");
62 productPriceLabel.setBounds(60, 240, 140, 30);
63 productPriceLabel.setFont(Theme.FONT_REGULAR);
64 panel.add(productPriceLabel);
65
66 productQtLabel = new JLabel("Quantity: ");
67 productQtLabel.setBounds(60, 290, 140, 30);
68 productQtLabel.setFont(Theme.FONT_REGULAR);
69 panel.add(productQtLabel);
70
71 productNameTF = new JTextField();
72 productNameTF.setBounds(180, 190, 220, 30);
73 productNameTF.setFont(Theme.FONT_INPUT);
74 panel.add(productNameTF);
75
76 productPriceTF = new JTextField();
77 productPriceTF.setBounds(180, 240, 220, 30);
78 productPriceTF.setFont(Theme.FONT_INPUT);
79 panel.add(productPriceTF);
80
81 productQtTF = new JTextField();
82 productQtTF.setBounds(180, 290, 220, 30);
83 productQtTF.setFont(Theme.FONT_INPUT);
84 panel.add(productQtTF);
85
86 buttonAdd = new JButton("Add");
87 buttonAdd.setBounds(60, 340, Theme.BUTTON_PRIMARY_WIDTH,30);
88 buttonAdd.setFont(Theme.FONT_BUTTON);
89 buttonAdd.setBackground(Theme.BACKGROUND_BUTTON_PRIMARY);
90 buttonAdd.setForeground(Theme.COLOR_BUTTON_PRIMARY);
91 buttonAdd.addActionListener(this);
92 panel.add(buttonAdd);
93
94 header = new JLabel();
95 header.setBackground(Theme.BACKGROUND_HEADER);
96 header.setOpaque(true);
97 header.setBounds(0, 0, Theme.GUI_WIDTH, 75);
98 panel.add(header);
99
100 this.add(panel);
101 }
102
103 public void actionPerformed(ActionEvent ae) {
104 if (ae.getSource().equals(buttonLogout)) {
105 this.setVisible(false);
106 new LoginActivity().setVisible(true);
107 }
108 else if (ae.getSource().equals(buttonBack)) {
109 this.setVisible(false);
110 new ViewProductActivity(new EmployeeActivity(employee.getUserId()), employee).setVisible(true);
111 }
112 else if (ae.getSource().equals(buttonAdd)) {
113 try {
114 Product p = new Product();
115 p.setProductName(productNameTF.getText().trim());
116 p.setPrice(Double.parseDouble(productPriceTF.getText()));
117 p.setQuantity(Integer.parseInt(productQtTF.getText()));
118 p.createProduct();
119 productNameTF.setText("");
120 productPriceTF.setText("");
121 productQtTF.setText("");
122 if (!activity.keywordTF.getText().trim().isEmpty())
123 activity.table.setModel(Product.searchProduct(activity.keywordTF.getText().trim(), activity.byWhatCB.getSelectedItem().toString()));
124 else
125 activity.table.setModel(Product.searchProduct("", "By Name"));
126 }
127 catch (NumberFormatException e) {
128 JOptionPane.showMessageDialog(this,"Enter price/quantity correctly!");
129 }
130 catch (IllegalArgumentException e) {
131 JOptionPane.showMessageDialog(this,e.getMessage());
132 }
133 }
134 else {}
135 }
136 }